package com.sxz.sbs.module.message;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.message.adapter.MessageAdapter;
import com.sxz.sbs.module.message.mode.MessageBean;
import com.sxz.sbs.module.message.presenter.MessagePresenter;
import com.sxz.sbs.module.message.view.MessageView;
import com.sxz.sbs.router.RouterUrl;
import com.sxz.sbs.widget.CommonTitle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 消息列表
 * @author tian
 */
@Route(path = RouterUrl.MESSAGE_LIST)
public class MessageActivity extends BaseActivity<MessagePresenter> implements MessageView {
    @Autowired
    int msgType;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private MessageAdapter adapter;
    private List<MessageBean.ListBean> dataList = new ArrayList<>();
    private int currentPage = 1;
    private boolean isList = true;
    private int handleIndex;
    private int handleTag ;
    @Override
    protected void init() {
        ARouter.getInstance().inject(this);
        switch (msgType) {
            case 0:
                new CommonTitle.Builder(this).setTitle("出错了...").showMore(false).build().show();
                break;
            case 1:
                new CommonTitle.Builder(this).setTitle("通知类消息").showMore(false).build().show();
                break;
            case 2:
                new CommonTitle.Builder(this).setTitle("审批类消息").showMore(false).build().show();
                break;
            case 3:
                new CommonTitle.Builder(this).setTitle("提醒类消息").showMore(false).build().show();
                break;
            default:
                break;
        }
        if (msgType != 0) {
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            refreshLayout.setColorSchemeResources(R.color.color_main);
            refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    currentPage = 1;
                    getMessageList();
                }
            });
            refreshLayout.setRefreshing(true);
            getMessageList();
            adapter = new MessageAdapter(dataList);
            recyclerView.setAdapter(adapter);
            adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    currentPage ++ ;
                    getMessageList();
                }
            },recyclerView);
            adapter.setOnMessageHandleListener(new MessageAdapter.OnMessageHandleListener() {
                @Override
                public void onRefuseClick(View view, int position, MessageBean.ListBean item) {
                    handleIndex = position;
                    handleTag = 0;
                    handleMessage(item.getId(),0);
                }

                @Override
                public void onAgreeClick(View view, int position, MessageBean.ListBean item) {
                    handleIndex = position;
                    handleTag = 1;
                    handleMessage(item.getId(),1);
                }

            });
            stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
                @Override
                public void onRetry(int state) {
                    refreshLayout.setRefreshing(true);
                    getMessageList();
                }
            });

        }

    }

    /**
     * 获取数据
     */
    private void getMessageList () {
        isList = true;
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("messageType",msgType);
        map.put("pageNumber",currentPage);
        map.put("pageSize", AppConstant.PAGE_SIZE);
        mPresenter.getMessageList(map);
    }

    /**
     * 处理
     * @param id
     * @param result
     */
    private void handleMessage (int id,int result) {
        isList = false;
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("messageId",id);
        map.put("handleResult",result);
        mPresenter.handleMessage(map);

    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    protected MessagePresenter createPresenter() {
        return new MessagePresenter();
    }

    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
    }
    @Override
    public void showError(int errorTag, String msg) {
        if (isList) {
            refreshLayout.setRefreshing(false);
            if (currentPage == 1) {
                if (errorTag == AppConstant.NET_ERROR_CODE) {
                    stateLayout.showNetwork();
                } else {
                    stateLayout.showEmpty();
                }
            } else {
                adapter.loadMoreFail();
            }
        } else {
            super.showError(errorTag,msg);
        }

    }
    @Override
    public void onGetMessageListSuccess(MessageBean data) {
        if (mIsLive ){
            refreshLayout.setRefreshing(false);
            if (data != null && data.getList() != null && data.getList().size() >0 ) {
                stateLayout.showContent();
                if (currentPage == 1) {
                    dataList.clear();
                    dataList.addAll(data.getList());
                    adapter.notifyDataSetChanged();
                } else {
                    dataList.addAll(data.getList());
                    adapter.notifyDataSetChanged();
                }
                adapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    dataList.clear();
                    adapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    if (dataList.size() > AppConstant.PAGE_SIZE) {
                        adapter.loadMoreEnd();
                    } else {
                        adapter.loadMoreEnd(true);
                    }
                }
            }
        }
    }

    @Override
    public void onHandleMessageSuccess() {
        if (mIsLive) {
           if (handleTag == 0) {
               dataList.get(handleIndex).setVal_Status(4);
           } else {
               dataList.get(handleIndex).setVal_Status(3);
           }
           adapter.notifyDataSetChanged();
        }
    }
}
